package cn.zx.rmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.selector.SelectMessageQueueByHash;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: cn.zx.rmq.producer
 * @ClassName: SpringbootProducer
 * @Author: Think
 * @CreateTime: 2021/6/30 10:46
 * @Description:
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SpringbootOrderProducer {

    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Test
    public void send() throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        String msg1 = "客户充值1000元！！！";
        String msg2 = "客户消费800元！！！";
        Message sendMsg1 = new Message("ORDER","about-money", "123",msg1.getBytes());
        Message sendMsg2 = new Message("ORDER","about-money", "123",msg2.getBytes());
        //默认3秒超时
        SendResult sendResult1 = defaultMQProducer.send(sendMsg1, new SelectMessageQueueByHash(), sendMsg1.getKeys());
        SendResult sendResult2 = defaultMQProducer.send(sendMsg2, new SelectMessageQueueByHash(), sendMsg2.getKeys());
        log.info("消息发送响应信息：{} & {}", sendResult1.toString(), sendResult2.toString());
    }
}
